package per.hx.bs.yw.serviceimpl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.domain.Tree;
import per.hx.bs.constant.Setting;
import per.hx.bs.entity.sys.SysRoleDO;
import per.hx.bs.entity.sys.SysUserDO;
import per.hx.bs.entity.yw.YwAppraisalDataDO;
import per.hx.bs.entity.yw.YwAppraisalRecordDO;
import per.hx.bs.entity.yw.YwAppraisalUserDO;
import per.hx.bs.sys.dao.SysRoleDao;
import per.hx.bs.sys.dao.SysUserDao;
import per.hx.bs.sys.dao.SysUserRoleDao;
import per.hx.bs.util.BuildTree;
import per.hx.bs.vo.AppraisalUserVO;
import per.hx.bs.yw.dao.YwAppraisalDataDao;
import per.hx.bs.yw.dao.YwAppraisalRecordDao;
import per.hx.bs.yw.dao.YwAppraisalUserDao;

import java.util.*;

/**
 * @author HuXing
 * @date 2018/4/16  12:18
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "yw-service/appraisalUser")
public class YwAppraisalUserApiServiceImpl {

    private final
    YwAppraisalUserDao appraisalUserDao;
    private final
    YwAppraisalRecordDao recordDao;
    private final
    YwAppraisalDataDao dataDao;
    private final
    SysUserDao userDao;
    private final
    SysRoleDao roleDao;
    private final
    SysUserRoleDao sysUserRoleDao;


    @Autowired
    public YwAppraisalUserApiServiceImpl(
            YwAppraisalUserDao appraisalUserDao,
            YwAppraisalRecordDao recordDao,
            YwAppraisalDataDao dataDao,
            SysUserDao userDao, SysRoleDao roleDao,
            SysUserRoleDao sysUserRoleDao) {
        this.appraisalUserDao = appraisalUserDao;
        this.recordDao = recordDao;
        this.dataDao = dataDao;
        this.userDao = userDao;
        this.roleDao = roleDao;
        this.sysUserRoleDao = sysUserRoleDao;
    }

    /**
     * 需考核用户
     *
     * @return 用户信息
     */

    @GetMapping(value = "/userTree")
    public String getUserTree(@RequestParam Long userId) {

        SysUserDO sysUserDO = userDao.selectById(userId);
        List<Long> longs = sysUserRoleDao.listRolesId(userId);
        List<SysRoleDO> roleDOS = roleDao.selectBatchIds(longs);
        Map<String, Object> map = new HashMap<>(1);
        for (SysRoleDO roleDO : roleDOS) {
            if (Setting.ROLE_USER_SIGN_D.equals(roleDO.getRoleSign().trim())) {
                map.put("deptId", sysUserDO.getDeptId());
                break;
            }
        }
        List<Tree<SysUserDO>> trees = new ArrayList<>();
        List<SysUserDO> sysUserDOS = appraisalUserDao.appraisalUser(map);
        for (SysUserDO userDO : sysUserDOS) {
            Tree<SysUserDO> tree = new Tree<>();
            tree.setId(userDO.getUserId().toString());
            tree.setText(userDO.getName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            state.put("mType", "user");
            tree.setState(state);
            trees.add(tree);
        }
        return JSON.toJSONString(BuildTree.build(trees, Setting.APPRAISAL_USER_TREE));
    }

    /**
     * 获取用户是否加入考核
     *
     * @param map 查询条件
     * @return 封装数据
     */
    @PostMapping(value = "/userList")
    public List<AppraisalUserVO> userList(@RequestBody Map<String, Object> map) {
        return appraisalUserDao.userList(map);
    }

    /**
     * 统计数量
     *
     * @param map 查询条件
     * @return 用户数量
     */
    @PostMapping(value = "/userCount")
    public int userCount(@RequestBody Map<String, Object> map) {
        return appraisalUserDao.userCount(map);
    }

    /**
     * 人员更改
     *
     * @param userId 用户id
     * @return 影响行数
     */
    @GetMapping(value = "/userChange/{userId}")
    public int userChange(@PathVariable("userId") Long userId) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("user_id", userId);
        System.out.println(appraisalUserDao.selectByMap(map).toString());
        if (appraisalUserDao.selectByMap(map).isEmpty()) {
            return appraisalUserDao.insert(new YwAppraisalUserDO(userId));
        } else {
            return appraisalUserDao.deleteByMap(map);
        }

    }

    /**
     * 获取用户考核结果列表
     *
     * @param map 查询条件
     * @return 封装数据
     */
    @PostMapping(value = "/list")
    public List<YwAppraisalRecordDO> list(@RequestBody Map<String, Object> map) {
        return recordDao.list(map);
    }

    /**
     * 统计用户考核结果数量
     *
     * @param map 查询条件
     * @return 数量
     */
    @PostMapping(value = "/count")
    public int count(@RequestBody Map<String, Object> map) {
        return recordDao.count(map);
    }


    /**
     * 获取考核详情
     *
     * @param id record id
     * @return 考核信息
     */
    @GetMapping(value = "/record/{id}")
    public YwAppraisalRecordDO get(@PathVariable("id") Long id) {
        return recordDao.selectById(id);
    }

    /**
     * 插入考核结果
     *
     * @param recordDO 考核结果
     * @return 影响行数
     */
    @PostMapping(value = "/record")
    public Integer insertRecord(@RequestBody YwAppraisalRecordDO recordDO) {


        return recordDao.insert(recordDO);
    }

    /**
     * 删除考核记录
     *
     * @param id 记录id
     * @return 影响行数
     */
    @DeleteMapping(value = "/record/{id}")
    public Integer delete(@PathVariable("id") Long id) {
        YwAppraisalRecordDO recordDO = recordDao.selectById(id);
        YwAppraisalDataDO dataDO = dataDao.selectById(recordDO.getFileId());
        if (null != dataDO) {
            dataDO.deleteById(dataDO.getId());
        }
        return recordDao.deleteById(id);
    }

    /**
     * 修改考核记录
     *
     * @param recordDO recordDO
     * @return 影响行数
     */
    @PutMapping(value = "/record")
    public Integer updateRecord(@RequestBody YwAppraisalRecordDO recordDO) {
        return recordDao.updateById(recordDO);
    }
}
